<?php
class Articles_model extends Model{

	function Common_model(){
		parent::Model();		
	}
	
	/**
	 * Get list in article
	 * @author tien.nguyen
	 */
	public function getListArticle($cate_code, $start = 0){
		$childCode = $this->_getChildItem($cate_code);
		
		$this->db->select();
		$this->db->from('news');
		$this->db->join('news_categories', 'news_categories.NewsCategoryId = 
			news.CategoryId');
		
		//set child code
		if(count($childCode) > 0){
			$this->db->where('news_categories.NewsCategoryCode = "' . $cate_code . '" 
				OR news.CategoryId IN ("' . implode('","', $childCode).'")');
		}else{
			$this->db->where('news_categories.NewsCategoryCode = "' . $cate_code . '"');
		}
		//$this->db->where('news_categories.NewsCategoryCode = "'.$cate_code.'"');
		$this->db->order_by('news.Ordering');
		$this->db->where('news.Visible = 1');
		$this->db->limit($this->config->item('record_limit'), $start);
		$query  = $this->db->get();
		return $query->result_array();
	}
	
	/**
	 * Get list other article
	 * @author tien.nguyen
	 */
	public function getListOtherArticle($cate_code, $currentArticleId){
		$childCode = $this->_getChildItem($cate_code);
		
		$this->db->select();
		$this->db->from('news');
		$this->db->join('news_categories', 'news_categories.NewsCategoryId = 
			news.CategoryId');
		
		if(count($childCode) > 0){
			$this->db->where('news_categories.NewsCategoryCode = "' . $cate_code . '" 
				OR news.CategoryId IN ("' . implode('","', $childCode).'")');
		}else{
			$this->db->where('news_categories.NewsCategoryCode = "' . $cate_code . '"');
		}
		$this->db->order_by('news.Ordering');
		$this->db->where('news.Visible = 1');
		$this->db->limit($this->config->item('record_limit_other_news'), 0);
		$this->db->where('news.NewsId <> '.$currentArticleId);
		
		$query  = $this->db->get();
		return $query->result_array();
		
	}

	/**
	 * Get child item
	 * @author tien.nguyen
	 */
	private function _getChildItem($cateCode){
//		$this->load->model('Articles_categories_model', null, true);
//		$list = $this->Articles_categories_model->getChild($cateCode);
		
		$modelCate = new Articles_categories_model();
		$list = $modelCate->getChild($cateCode);
			
		$res = array();
		if(is_array($list)){
			foreach ($list as $item){
				array_push($res, $item['NewsCategoryId']);
			}	
		}
		return $res;
	}
	
	/**
	 * Get count for article
	 * @author tien.nguyen
	 */
	public function getCountArticle($cate_code){
		$this->db->select();
		$this->db->from('news');
		$this->db->join('news_categories', 'news_categories.NewsCategoryId = 
			news.CategoryId');
		$this->db->where('news_categories.NewsCategoryCode = "'.$cate_code.'"');
		$this->db->where('news.Visible = 1');
		$this->db->order_by('news.Ordering');
		$query = $this->db->get();
		$res = $query->num_rows();
        return $res;
	}
    
    /**
     * get list hot news in home
     * @author tien.nguyen
     */
    public function getHotNewsHome(){
        $this->db->select();
        $this->db->from('news');
        $this->db->join('news_categories', 'news_categories.NewsCategoryId = 
        	news.CategoryId');
        $this->db->limit(5);
        $this->db->where('news_categories.NewsCategoryCode = "tin_tuc"');
        $this->db->order_by('news.Ordering');
        $query = $this->db->get();
        
        return $query->result_array(); 
    }
    
    /**
     * get article by article id
     * @author tien.nguyen
     * @param  int $articleId
     */
   	public function getArticle($articleId){
   		$this->db->select();
   		$this->db->from('news');
   		$this->db->where('news.Newsid = '.$articleId);
   		$query = $this->db->get();
   		
   		return $query->row_array();
   	}
    
    /**
     * Get news in list id
     * @author tien.nguyen
     * @param array
     */
    public function getInListId($list){
        $this->db->select();
        $this->db->from('news');
        $this->db->where("news.NewsId IN ('". implode("','", $list). "')");
        $query = $this->db->get();        
        return $query->result_array();
    }
    
    /**
     * Set home news by in list
     * @author tien.nguyen
     */
    public function getHomeNewsByInList($res){
        $arr = array();
        foreach($res as $item){
            switch($item['NewsId']){
                case 13 : {
                    $arr['hoi_cho'] = $item;
                    break;
                }
                case 12 : {
                    $arr['hoi_thao'] = $item;
                    break;
                }
                case 14 : {
                    $arr['giai_tri'] = $item;
                    break;
                }
            }
        }        
        return $arr;
    }
}